package com.lankystar.logistucs.spring.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lankystar.logistucs.spring.dao.ICompanyDao;
import com.lankystar.logistucs.spring.dao.ITruckDao;
import com.lankystar.logistucs.spring.dao.ITruckStatusDao;
import com.lankystar.logistucs.spring.dao.IUserDao;
import com.lankystar.logistucs.spring.entity.Company;
import com.lankystar.logistucs.spring.entity.Truck;
import com.lankystar.logistucs.spring.entity.TruckStatus;
import com.lankystar.logistucs.spring.entity.TruckType;
import com.lankystar.logistucs.spring.entity.UserType;
import com.lankystar.logistucs.spring.service.TruckService;


@Service
public class TruckServiceImpl implements TruckService {

	@Autowired
	private ITruckStatusDao truckStatusDao;
	@Autowired
	private ITruckDao truckDao;
	@Autowired
	private IUserDao userDao;
	@Autowired
	private ICompanyDao companyDao;
	@Override
	public List<Map<String, Object>> queryTrucklist(Integer userId) {
		List<Map<String, Object>> maplist = new ArrayList<>();
		List<Object[]>  trucklist = truckDao.selectCanUserTrucklist(userId);
		for (Object[] obs : trucklist) {
			Map<String, Object> map = new HashMap<>();
			if(userId==null){
				Truck truck = (Truck) obs[0];
				TruckType truckType = (TruckType) obs[2];
				map.put("truckId", truck.getTruckId());
				map.put("cardNu", truck.getCardNu());
				map.put("plateNu", truck.getPlateNu());
				map.put("loadHeavy", truckType.getLoadHeavy());
				map.put("address", truck.getAddress());
				maplist.add(map);
			}else{
				Truck truck = (Truck) obs[2];
				TruckType truckType = (TruckType) obs[4];
				map.put("truckId", truck.getTruckId());
				map.put("cardNu", truck.getCardNu());
				map.put("plateNu", truck.getPlateNu());
				map.put("loadHeavy", truckType.getLoadHeavy());
				map.put("address", truck.getAddress());
				maplist.add(map);
			}
		}
		return maplist;
	}

	@Override
	public void addTruck(Truck record,int userId) {
		TruckStatus truckStatus = truckStatusDao.selectTruckStatusById(1);
		Company company = companyDao.selectCompany(userId);
		record.setCompany(company);
		record.setCreateTime(new Date().getTime());
		record.setTruckStatus(truckStatus);
		
	}

	@Override
	public List<Map<String, Object>> queryTruckAndStatuslist(int userId) {
		List<Map<String, Object>> maplist = new ArrayList<>();
		Object[] ob = userDao.selectUserAndUserTypeById(userId);
		UserType ut = (UserType) ob[1];
		if(ut.getUserKey()==101){
			List<Object[]>  trucklist = truckDao.selectTruckAndStatuslist(userId);
			for (Object[] obs : trucklist) {
				Map<String, Object> map = new HashMap<>();
				Truck truck = (Truck) obs[2];
				TruckStatus truckStatus = (TruckStatus) obs[3];
				TruckType truckType = (TruckType) obs[4];
				map.put("truckId", truck.getTruckId());
				map.put("cardNu", truck.getCardNu());
				map.put("plateNu", truck.getPlateNu());
				map.put("loadHeavy", truckType.getLoadHeavy());
				map.put("truckStatusKey", truckStatus.getTruckStatusKey());
				map.put("truckStatusValue", truckStatus.getTruckStatusValue());
				maplist.add(map);
			}
		}
		return maplist;
	}

}
